package com.example.zhongshuishi.service.implService;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.alibaba.nacos.shaded.com.google.gson.JsonParser;
import com.example.zhongshuishi.entity.InvoiceDto;
import com.example.zhongshuishi.entity.StudentDto;
import com.example.zhongshuishi.mapper.StudentMapper;
import com.example.zhongshuishi.taxs_api.RequestKit;
import com.example.zhongshuishi.utils.QRCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Random;
import java.util.UUID;

@Service
public class InvoiceServiceImpl {
    @Value("${tax.url}")
    private String tax_url;
    @Value("${tax.publickey}")
    private String tax_publickey;
    @Value("${tax.privatekey}")
    private String tax_privatekey;
    @Value("${tax.mock}")
    private String tax_mock;
    private final String TAX_NO = "91110108802079937H";
    private final String TAX_APPID = "18e81c37-17d4-4f0d-8ab5-6ccf874c22bf";



    @Autowired
    private StudentMapper studentMapper;

    public JSONObject invoiceApi(InvoiceDto invoiceDto) throws UnsupportedEncodingException {
       String dj=invoiceDto.getHjje();
        // 1. 构造 mx（明细）部分
        JSONArray mxArray = new JSONArray();
        JSONObject mxItem = new JSONObject();
        mxItem.put("xh", invoiceDto.getXh());
        mxItem.put("fphxz", invoiceDto.getFphxz());
        mxItem.put("hsbz", invoiceDto.getHsbz());
        mxItem.put("se", invoiceDto.getSe());
        mxItem.put("yhzcbs", invoiceDto.getYhzcbs());
        mxItem.put("spmc", invoiceDto.getSpmc());
        mxItem.put("sl", invoiceDto.getSl());
        mxItem.put("dw", "元");
        if (dj!=null){
            mxItem.put("spsl", "1");
        }
        mxItem.put("dj", dj);
        mxItem.put("je", invoiceDto.getJe());
        mxItem.put("spbm", invoiceDto.getSpbm());
        mxArray.add(mxItem);

        JSONObject input = new JSONObject();
        input.put("ghdwdz", invoiceDto.getGhdwdz());
        input.put("ghdwmc",invoiceDto.getGhdwmc());
        input.put("kplx", invoiceDto.getKplx());
        input.put("hjse", invoiceDto.getHjse());
        input.put("ghdwyh",invoiceDto.getGhdwyh());
        input.put("ghdwzh", invoiceDto.getGhdwzh());
        input.put("xhdwdh", invoiceDto.getXhdwdh());

        input.put("mx", mxArray); // 添加明细

        input.put("gmf_dzyx", invoiceDto.getGmf_dzyx());
        input.put("kpr", invoiceDto.getKpr());
        input.put("hjje", invoiceDto.getHjje());
        input.put("ghdwdh", invoiceDto.getGhdwdh());
        input.put("fplxdm", invoiceDto.getFplxdm());
        input.put("ghdwsbh", invoiceDto.getGhdwsbh());
        input.put("bz",invoiceDto.getBz());
        input.put("gfxxconfirm", invoiceDto.getGfxxconfirm());
        input.put("jshj", invoiceDto.getJshj());
        input.put("xhdwyh", invoiceDto.getXhdwyh());
        input.put("zyspmc", invoiceDto.getZyspmc());
        input.put("tspz", invoiceDto.getTspz());
        input.put("xhdwzh", invoiceDto.getXhdwzh());

        //负数发票必传
        if("1".equals(invoiceDto.getKplx())){
            input.put("hzfpxxqrdid", invoiceDto.getXxbid());
            input.put("xxbid", invoiceDto.getXxbid());
        }

        // 3. 构造最终的 JSON
        JSONObject requestJson = new JSONObject();
        requestJson.put("async", "true");
        requestJson.put("mock", tax_mock);
        requestJson.put("input", input);
        requestJson.put("nsrsbh", TAX_NO); // 可以改成动态传入
        requestJson.put("appid", TAX_APPID); // 可以改成动态传入
        requestJson.put("serviceid", "FPKJ"); // 可以改成动态传入
        requestJson.put("sid", invoiceDto.getSid()); // 可以改成动态传入

        System.out.println(requestJson);
        //
        JSONObject ret = RequestKit.request(tax_url, tax_publickey, tax_privatekey, requestJson.toJSONString());

        return ret;

    }

    //人脸识别
    public String identifyApi(InvoiceDto invoiceDto) throws UnsupportedEncodingException {
        String sid= UUID.randomUUID().toString().replace("-", "");
        // 2. 构造 input 部分
        JSONObject input = new JSONObject();
        input.put("rlsblx", "0");

        // 3. 构造最终的 JSON
        JSONObject requestJson = new JSONObject();
        requestJson.put("async", "true");
        requestJson.put("mock", tax_mock);
        requestJson.put("input", input);
        requestJson.put("nsrsbh", TAX_NO);
        requestJson.put("appid", TAX_APPID);
        requestJson.put("serviceid", "HQRLSBEWM"); // 接口ID

        requestJson.put("sid",sid); // 订单ID
        //
        JSONObject ret = RequestKit.request(tax_url, tax_publickey, tax_privatekey, requestJson.toJSONString());

        JSONObject reObj=JSONObject.parseObject(String.valueOf(ret));
        JSONObject dataObj=reObj.getJSONObject("data");
        String erm=dataObj.getString("ewm");

        //获取人脸识别结果
        String rzid=dataObj.getString("rzid");
        input.put("rzid", rzid);
        input.put("rlsblx", "0");
        String sid1= UUID.randomUUID().toString().replace("-", "");
        JSONObject requestJson1 = new JSONObject();
        requestJson1.put("async", "true");
        requestJson1.put("mock", tax_mock);
        requestJson1.put("input", input);
        requestJson1.put("nsrsbh",TAX_NO); // 可以改成动态传入
        requestJson1.put("appid", TAX_APPID); // 可以改成动态传入
        requestJson1.put("serviceid", "HQRLSBJG"); // 可以改成动态传入
        requestJson1.put("sid", sid1); // 可以改成动态传入
        JSONObject ret1 = RequestKit.request(tax_url, tax_publickey, tax_privatekey, requestJson1.toJSONString());
        JSONObject reObj1=JSONObject.parseObject(String.valueOf(ret1));
        JSONObject dataObj1=reObj1.getJSONObject("data");
        String slzt=dataObj1.getString("slzt"); //扫脸状态

        if ("3".equals(slzt)){
            String jsonString = ret.toString();
            return jsonString;
        }else {
            String payData=erm;
            // 使用 QRCodeUtil 生成二维码图片
            int width = 200; // 二维码宽度
            int height = 200; // 二维码高度
            return  QRCodeUtil.generateQRCode(payData, width, height);
        }
    }


    //红冲
    public JSONObject redPunchApi(InvoiceDto invoiceDto) {
        try{
            JSONArray mxArray = new JSONArray();
            JSONObject mxItem = new JSONObject();
            mxItem.put("dj", "");
            mxItem.put("dw", "元");
            mxItem.put("je", "");
            mxItem.put("ggxh", "");
            mxItem.put("lzmxxh","");
            mxItem.put("se", "");
            mxItem.put("sl", "1");
            mxItem.put("slv", "0.3");
            mxItem.put("spbm", "");
            mxItem.put("xmmc", "");
            mxArray.add(mxItem);

            // 2. 构造 input 部分
            JSONObject input = new JSONObject();
            input.put("chyydm", invoiceDto.getChyydm());
            input.put("fplxdm","");
            input.put("fplydm", "");
            input.put("gmfnsrsbh", TAX_NO);
            input.put("hzcxje", "");
            input.put("hzcxse", "");

            input.put("hzqrxxmxlist", mxArray);

            input.put("sqly", "0");
            input.put("yzzfpdm", "");
            input.put("yzzfphm", "");
            input.put("yfphm", invoiceDto.getYfphm());
            input.put("ykprq", invoiceDto.getYkprq());
            input.put("xsfnsrsbh", TAX_NO);
            input.put("tdyslxdm", "");

            // 3. 构造最终的 JSON
            JSONObject requestJson = new JSONObject();
            requestJson.put("async", "true");
            requestJson.put("mock", tax_mock);
            requestJson.put("input", input);
            requestJson.put("nsrsbh", TAX_NO);
            requestJson.put("appid", TAX_APPID);
            requestJson.put("serviceid", "HZTZDSQ"); // 接口ID

            requestJson.put("sid",invoiceDto.getSid()); // 订单ID

            JSONObject ret = RequestKit.request(tax_url, tax_publickey, tax_privatekey, requestJson.toJSONString());
            return ret;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    //红冲查询
    public JSONObject redPunchGet(InvoiceDto invoiceDto) {
        try{
            String sid= UUID.randomUUID().toString().replace("-", "");
            // 2. 构造 input 部分
            JSONObject input = new JSONObject();

            input.put("sqly", "0");
            input.put("xxbid", invoiceDto.getXxbid());
            input.put("xxbbh", invoiceDto.getXxbbh());
            input.put("kprqq", invoiceDto.getKprqq());
            input.put("kprqz", invoiceDto.getKprqz());
            input.put("pageSize", invoiceDto.getPageSize());
            input.put("pageNumber", invoiceDto.getPageNumber());

            // 3. 构造最终的 JSON
            JSONObject requestJson = new JSONObject();
            requestJson.put("async", "true");
            requestJson.put("mock", tax_mock);
            requestJson.put("input", input);
            requestJson.put("nsrsbh", TAX_NO);
            requestJson.put("appid", TAX_APPID);
            requestJson.put("serviceid", "HZTZDZBCX"); // 接口ID

            requestJson.put("sid",sid); // 订单ID

            JSONObject ret = RequestKit.request(tax_url, tax_publickey, tax_privatekey, requestJson.toJSONString());
            return ret;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    //通用查询
    public JSONObject taxInfo(InvoiceDto invoiceDto) {
        try{
            // 2. 构造 input 部分
            JSONObject input = new JSONObject();

            // 3. 构造最终的 JSON
            JSONObject requestJson = new JSONObject();
            requestJson.put("async", "true");
            requestJson.put("mock", tax_mock);
            requestJson.put("input", input);
            requestJson.put("nsrsbh", TAX_NO);
            requestJson.put("appid", TAX_APPID);
            requestJson.put("serviceid", "CXJG"); // 接口ID

            requestJson.put("sid",invoiceDto.getSid()); // 订单ID

            JSONObject ret = RequestKit.request(tax_url, tax_publickey, tax_privatekey, requestJson.toJSONString());
            return ret;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    //开票链接
    public JSONObject dowloadLink(InvoiceDto invoiceDto) throws UnsupportedEncodingException {
        try{
            String sid= UUID.randomUUID().toString().replace("-", "");
            // 2. 构造 input 部分
            JSONObject input = new JSONObject();
            input.put("qdfphm", invoiceDto.getQdfphm());
            input.put("kprq", invoiceDto.getYkprq());

            // 3. 构造最终的 JSON
            JSONObject requestJson1 = new JSONObject();
            requestJson1.put("async", "true");
            requestJson1.put("mock", tax_mock);
            requestJson1.put("input", input);
            requestJson1.put("nsrsbh", TAX_NO);
            requestJson1.put("appid", TAX_APPID);
            requestJson1.put("serviceid", "HQFPXZDZ"); // 接口ID
            requestJson1.put("sid",sid); // 订单ID

            JSONObject ret1 = RequestKit.request(tax_url, tax_publickey, tax_privatekey, requestJson1.toJSONString());
            return ret1;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }
    public List<StudentDto> selectInformation(InvoiceDto invoiceDto) {
        String name=invoiceDto.getGhdwmc();
        String phone=invoiceDto.getGhdwdh();
        return studentMapper.selectInformation(name,phone);
    }
}
